#include "pix5core.h"

App* App::_app = NULL;

App::App()
{
}

App::~App()
{
}



void App::main()
{	
	try
	{
		init();
	}
	catch(Error e)
	{
		try
		{
			shutdown();
		}
		catch(Error e2)
		{
			printf("\033[31mA pix5 initialization error occured, which was followed by a second error during shutdown.\033[m\n");
			printf("\033[31mOriginal initialization error:\033[m\n   %s\n",(const char*)e);
			printf("\033[31mShutdown error:\033[m\n   %s\n",(const char*)e2);

			return;
		}

		printf("\033[31mpix5 initialization error:\033[m\n   %s\n",(const char*)e);
		return;
	}


	try
	{
		run();
	}
	catch(Error e)
	{
		try
		{
			shutdown();
		}
		catch(Error e2)
		{
			printf("\033[31mAn unhandled error occured, which was followed by a second error during shutdown.\033[m\n");
			printf("\033[31mOriginal unhandled error:\033[m\n   %s\n",(const char*)e);
			printf("\033[31mShutdown error:\033[m\n   %s\n",(const char*)e2);

			return;
		}

		printf("\033[31mUnhandled error:\033[m\n   %s\n",(const char*)e);
		return;
	}

	try
	{
		shutdown();
	}
	catch(Error e)
	{
		printf("\033[31mpix5 shutdown error:\033[m\n   %s\n",(const char*)e);
		return;
	}
}

void App::init()
{
	#ifdef PIX5DEBUG
		Debug::init();
	#endif

	ThreadMan::init();
	IO::init();
	Platform::init();
	DB::init();

	onInit();
}

void App::shutdown()
{
	onShutdown();

	DB::shutdown();
	Platform::shutdown();
	ThreadMan::shutdown();
	IO::shutdown();
	
	#ifdef PIX5DEBUG
		Debug::shutdown();
	#endif
}

void App::run()
{
}


